package com.iceberg;

import javax.swing.*;
import java.awt.*;

public class AlgorithmFrame extends JFrame{
    private int canvasWidth;
    private int canvasHeight;


    public AlgorithmFrame(String title, int canvasWidth, int canvasHeight){
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        AlgorithmCanvas canvas = new AlgorithmCanvas();
        setContentPane(canvas);
        pack();

        setSize(canvasWidth, canvasHeight);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);
    }

    public AlgorithmFrame(String title){
        this(title, 1024, 768);
    }

    public int getCanvasWidth(){
        return canvasWidth;
    }

    public int getCanvasHeight(){
        return canvasHeight;
    }

    private Circle[] circles;

    public void render(Circle[] circles){
        this.circles = circles;
        repaint();
    }

    private class AlgorithmCanvas extends JPanel{

        public AlgorithmCanvas(){
            super(true);
        }

        @Override
        public void paintComponent(Graphics graphics){

            super.paintComponent(graphics);

            Graphics2D graphics2D = (Graphics2D)graphics;

            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            graphics2D.addRenderingHints(hints);

            AlgorithmVisualHelper.setStrokeWidth(graphics2D, 1);
            AlgorithmVisualHelper.setColor(graphics2D, Color.RED);

            for (Circle circle: circles) {
                if (circle.isFilled){
                    AlgorithmVisualHelper.fillCircle(graphics2D, circle.x, circle.y, circle.getR());
                }else {
                    AlgorithmVisualHelper.strokeCircle(graphics2D, circle.x, circle.y, circle.getR());
                }
            }

        }


        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }

    }
}
